using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Dynamic;
using System.Data.SqlClient;
using System.Configuration;
using Model;
using Common;

namespace DAL
{
    public class PositionDAL
    {
        public static int Save(PositionInfo info)
        {
            string sqls = string.Format("select ID from [position] where [Code] ='{0}'",info.Code);
            int id = DataConvert.ToInt(CommonDAL.ExecuteScalar(sqls));
            if (id > 0)
            {
                return -1;
            }
            else
            {
                string sql = string.Format("insert into [position]([Code],[IsNull],[Remarks],[Founder],[CreateDate]) values(@Code,@IsNull,@Remarks,@Founder,@CreateDate);select @@identity;");
                SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@Code", info.Code),
                new SqlParameter("@IsNull", info.IsNull),
                new SqlParameter("@Remarks", info.Remarks),
                new SqlParameter("@Founder", info.Founder),
                new SqlParameter("@CreateDate", info.CreateDate)
                };
                return DataConvert.ToInt(CommonDAL.ExecuteScalar(sql, paras));
            }
        }
        public static int Update(PositionInfo info)
        {
            string sqls = string.Format("select ID from [position] where [Code] ='{0}'",info.Code);
            int id = DataConvert.ToInt(CommonDAL.ExecuteScalar(sqls));
            if (id > 0)
            {
                return -1;
            }
            else
            {
                string sql = string.Format("update [position] set [Code]=@Code,[IsNull]=@IsNull,[Remarks]=@Remarks,[Founder]=@Founder where [ID]=@ID");
                SqlParameter[] paras = new SqlParameter[]{
                new SqlParameter("@Code", info.Code),
                new SqlParameter("@IsNull", info.IsNull),
                new SqlParameter("@Remarks", info.Remarks),
                new SqlParameter("@Founder", info.Founder),
                new SqlParameter("@ID", info.ID)
                };
                return CommonDAL.ExecuteNoneQuery(sql, paras) ? info.ID : 0;
            }
        }
        public static PositionInfo FindOneByID(int id)
        {
            DataRow dr = CommonDAL.FindOneByID(id, "[position]");
            return Simple_Data(dr);
        }
        public static List<PositionInfo> FindListByIDs(string ids)
        {
            DataTable dt = CommonDAL.FindListByIDs(ids, "[position]");
            List<PositionInfo> list = new List<PositionInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        public static List<PositionInfo> FindPage(string keyword, string position, string enddate, int startindex, int pagesize, string order, string desc, out int count)
        {
            StringBuilder filter = new StringBuilder();
            if (!string.IsNullOrEmpty(keyword))
            {
                filter.AppendFormat(" and [position].Founder like '%{0}%'", keyword);
            }
            if (!string.IsNullOrEmpty(position))
            {
                filter.AppendFormat(" and [position].Code like '%{0}%'", position);
            }
            DataTable dt = CommonDAL.FindPage(startindex, pagesize, order, desc, filter, "[position]", out count);
            List<PositionInfo> list = new List<PositionInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        public static List<PositionInfo> FindAll()
        {
            DataTable dt = CommonDAL.GetDataTable(string.Format("select * from [position]"));
            List<PositionInfo> list = new List<PositionInfo>();
            foreach (DataRow row in dt.Rows)
            {
                list.Add(Simple_Data(row));
            }
            return list;
        }
        public static PositionInfo FindOneByCode(String code)
        {
            DataRow dr = CommonDAL.GetDataRow(string.Format("select * from [position] where Code='" + code + "'"));
            return Simple_Data(dr);
        }
        private static PositionInfo Simple_Data(DataRow dr)
        {
            PositionInfo info = new PositionInfo();
            if (dr != null)
            {
                info.ID = DataConvert.ToInt(dr["ID"]);
                info.Code = DataConvert.ToString(dr["Code"]);
                info.IsNull = DataConvert.ToInt(dr["IsNull"]);
                info.Remarks = DataConvert.ToString(dr["Remarks"]);
                info.Founder = DataConvert.ToString(dr["Founder"]);
                info.CreateDate = DataConvert.ToDateTime(dr["CreateDate"]);
            }
            return info;
        }
    }
}

